	
	$(document).ready(function(e) {
		hoverSidebar('li.has-sub-menu');
		
		function hoverSidebar(selector){
			$(selector).find('.sub-menu').css({display: "inherit"});
			$(selector).find('.sub-menu').hide();
			var hoverTimeout;
			var hoverSelector;	
			$(selector).hover(function(){
				hoverSelector = this;
				if ( ! $(hoverSelector).hasClass('active')) {
					hoverTimeout = setTimeout(function(){
						$('ul', hoverSelector).slideDown(500);
					},500);
				}
			}, function(){
				if ( ! $(hoverSelector).hasClass('active')) {
					clearTimeout(hoverTimeout);
					$('ul', hoverSelector).stop(true,true).slideUp(500);
				}
			});
		}
		
		$("#navigation a").click(function(){
			var index_active = $(this).parents('.nav-menu').index();
			$.cookie("menustate", index_active);
		});
		if($.cookie("menustate") != null){
			$("#navigation .nav-menu:eq("+$.cookie("menustate")+")").addClass("active").find('.sub-menu').show();
			addclassNoCookie();
		}else{
			$("#navigation .nav-menu:eq(0)").addClass("active").find('.sub-menu').show();
			addclassNoCookie();
		}
		function addclassNoCookie(){
			var url = window.location.href;
			var selector = "#navigation .nav-menu";
			$(selector).each(function(index, element) {
				if($(this).find('.link-menu').attr('href') == url.split("?",1)){					
					$(selector).removeClass('active');	
					$(selector).find('.sub-menu').hide();									
					$(this).addClass("active").find('.sub-menu').show();
					$.cookie("menustate", $(this).index());		
				}else{					
					if($(this).hasClass('has-sub-menu')){
						$(this).find('.sub-menu li').each(function(index, element) {
							if($(this).find('a').attr('href') == url.split("?",1)){
								$(selector).removeClass('active');	
								$(selector).find('.sub-menu').hide();	
								$(this).parents('.nav-menu').addClass("active");
								$(this).parent().show();
								$.cookie("menustate", $(this).parents('.nav-menu').index());
							}
						});
					}
				}
			 });
		}
    });